package com.agileai.bpm.controller.instance;

import java.util.List;

import com.agileai.bpm.bizmoduler.instance.WfProcessInstanceManage;
import com.agileai.bpm.controller.emulation.ProcessHelper;
import com.agileai.bpm.webservice.BPMService;
import com.agileai.bpm.webservice.HandleWorkItem;
import com.agileai.domain.DataParam;
import com.agileai.domain.DataRow;
import com.agileai.hotweb.annotation.PageAction;
import com.agileai.hotweb.controller.core.BaseHandler;
import com.agileai.hotweb.domain.core.User;
import com.agileai.hotweb.renders.AjaxRenderer;
import com.agileai.hotweb.renders.LocalRenderer;
import com.agileai.hotweb.renders.RedirectRenderer;
import com.agileai.hotweb.renders.ViewRenderer;
import com.agileai.util.StringUtil;

public class WfProcessInstanceListHandler extends BaseHandler{
	public WfProcessInstanceListHandler(){
		super();
		this.serviceId = buildServiceId(WfProcessInstanceManage.class);
	}
	public ViewRenderer prepareDisplay(DataParam param){
		mergeParam(param);
		initParameters(param);
		this.setAttributes(param);
		List<DataRow> rsList = getService().findInstanceListRecords(param);
		this.setRsList(rsList);
		processPageAttributes(param);
		return new LocalRenderer(getPage());
	}
	public ViewRenderer doQueryAction(DataParam param){
		param.remove("WFP_ID");
		param.remove("WFP_NAME");
		return prepareDisplay(param);
	}
	
	@PageAction
	public ViewRenderer suspendInstance(DataParam param){
		String responseText = FAIL;
		try {
			String processId = param.get("WFP_ID");
			String processInstanceId = param.get("WFIP_ID");
			User user = (User)this.getUser();
			
			BPMService bpmService = ProcessHelper.instance().getBPMService();
			HandleWorkItem handleWorkItem = new HandleWorkItem();
			handleWorkItem.setProcessId(processId);
			handleWorkItem.setProcessInstanceId(processInstanceId);
			handleWorkItem.setUserCode(user.getUserCode());
			bpmService.suspendProcess(handleWorkItem);
			
			responseText = SUCCESS;
		} catch (Exception e) {
			log.error(e.getLocalizedMessage());
			responseText = FAIL;
		}
		return new AjaxRenderer(responseText);
	}

	@PageAction
	public ViewRenderer resumeInstance(DataParam param){
		String responseText = FAIL;
		try {
			String processId = param.get("WFP_ID");
			String processInstanceId = param.get("WFIP_ID");
			User user = (User)this.getUser();
			
			BPMService bpmService = ProcessHelper.instance().getBPMService();
			HandleWorkItem handleWorkItem = new HandleWorkItem();
			handleWorkItem.setProcessId(processId);
			handleWorkItem.setProcessInstanceId(processInstanceId);
			handleWorkItem.setUserCode(user.getUserCode());
			bpmService.resumeProcess(handleWorkItem);
			
			responseText = SUCCESS;
		} catch (Exception e) {
			log.error(e.getLocalizedMessage());
			responseText = FAIL;
		}		
		return new AjaxRenderer(responseText);
	}
	
	@PageAction
	public ViewRenderer terminateInstance(DataParam param){
		String responseText = FAIL;
		try {
			String processId = param.get("WFP_ID");
			String processInstanceId = param.get("WFIP_ID");
			User user = (User)this.getUser();
			
			BPMService bpmService = ProcessHelper.instance().getBPMService();
			HandleWorkItem handleWorkItem = new HandleWorkItem();
			handleWorkItem.setProcessId(processId);
			handleWorkItem.setProcessInstanceId(processInstanceId);
			handleWorkItem.setUserCode(user.getUserCode());
			bpmService.terminateProcess(handleWorkItem);
			
			responseText = SUCCESS;
		} catch (Exception e) {
			log.error(e.getLocalizedMessage());
			responseText = FAIL;
		}			
		return new AjaxRenderer(responseText);
	}	
	
	public ViewRenderer doBactToStatAction(DataParam param){
		return new RedirectRenderer(getHandlerURL(WfProcessInstanceStatHandler.class));
	}
	
	protected void processPageAttributes(DataParam param) {
		if (!StringUtil.isNullOrEmpty(param.get("WFP_ID"))){
			this.setAttribute("CUR_WFP_ID", param.get("WFP_ID"));
		}
		if (!StringUtil.isNullOrEmpty(param.get("WFP_NAME"))){
			this.setAttribute("CUR_WFP_NAME", param.get("WFP_NAME"));
		}
	}
	protected void initParameters(DataParam param) {
		if (!StringUtil.isNullOrEmpty(param.get("WFP_ID"))){
			param.put("CUR_WFP_ID", param.get("WFP_ID"));
		}
		if (!StringUtil.isNullOrEmpty(param.get("WFP_NAME"))){
			param.put("CUR_WFP_NAME", param.get("WFP_NAME"));
		}		
	}
	protected WfProcessInstanceManage getService() {
		return (WfProcessInstanceManage)this.lookupService(this.getServiceId());
	}
}	
